//
//  HNSH_TaskCenterManage.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/29.
//

#define JXTableHeaderViewHeight 326
#define JXheightForHeaderInSection  46

#import "HNSH_TaskCenterManage.h"
//view
#import "JXPagerView.h"
#import "JXCategoryTitleView.h"
#import "JXCategoryIndicatorLineView.h"

#import "HNSH_TaskCenterTopView.h"

//#import "HNSH_RewardToors.h"

//vc
#import "HNSH_TaskCenterViewController.h"
#import "HNSH_ExchangeNetServer.h"


@interface HNSH_TaskCenterManage ()<JXPagerViewDelegate, JXPagerMainTableViewGestureDelegate, JXCategoryViewDelegate>
/**  */
@property (nonatomic, strong) JXPagerView * pagerView;
/**  */
@property (nonatomic, strong) JXCategoryTitleView * categoryView;
/**  */
@property (nonatomic, strong) HNSH_TaskCenterTopView * headerView;

@end

@implementation HNSH_TaskCenterManage

//MARK: Cycle Methods
- (void)viewDidLoad{
    [super viewDidLoad];
    
    [self showNavTitle:@"任务中心" showLeftItem:YES];
    self.navView.backgroundColor = [UIColor clearColor];
    self.navView.titleLabel.textColor = [UIColor appWhiteLabelColor];
    [self.navView.leftButton setImage:[UIImage imageNamed:@"base_nav_back_white"] forState:UIControlStateNormal];
    [self addCategoryView];
    
        
    [self loadNewData];
    
    [self addAllViews];
    
    [self configTopView];
}

#pragma mark configUI
- (void)configTopView {
    UIImageView *imgV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, JXTableHeaderViewHeight)];
    imgV.image = [UIImage imageNamed:@"task_center_home_top_bg"];
    [self.view addSubview:imgV];
    [self.view sendSubviewToBack:imgV];
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#FDE4C4"];
}

- (void)addAllViews{
    [self.view addSubview:self.pagerView];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];

    self.pagerView.frame = CGRectMake(12, kNavBarAndStatusBarHeight, kScreenWidth-24, kScreenHeight - kNavBarAndStatusBarHeight);
}

//MARK: 获取所有代币
- (void)addCategoryView{
    self.categoryView.titles = @[@"任务列表", @"我的任务", @"过期任务"];
    [self.categoryView reloadDataWithoutListContainer];
    [self.pagerView reloadData];
    [self.view clipCornerWithView:self.categoryView cornerWidth:8 cornerType:UIRectCornerTopLeft|UIRectCornerTopRight];
}

- (void)loadNewData{
    [HNSH_ExchangeNetServer requestQuestHomeDataSuccess:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            HNSH_TaskCenterModel *model = [HNSH_TaskCenterModel yy_modelWithDictionary:responseObject.resultset];
            self.headerView.model = model;
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

//去完成任务
- (void)gotoAdVideo {
    WeakSelf(weakSelf);
    //激励视频
//    [[HNSH_RewardToors share] showADWithController:self complate:^(BOOL success) {
//        if (success) {
//            //完成任务
//            [HNSH_ExchangeNetServer requestQuestProgressWithKey:@"" success:^(ST_BaseNetModel * _Nullable responseObject) {
//                if (responseObject.code == 0) {
//                    [weakSelf loadNewData];
//                }
//            } Failure:^(NSError * _Nullable error) {
//                    
//            }];
//        }
//    }];
}

//MARK: Getter Methods
- (JXPagerView *)pagerView{
    if (!_pagerView) {
        _pagerView = [[JXPagerView alloc] initWithDelegate:self];
        _pagerView.mainTableView.gestureDelegate = self;
        _pagerView.mainTableView.backgroundColor = [UIColor clearColor];
        _pagerView.backgroundColor = [UIColor clearColor];
    }
    return _pagerView;
}

- (HNSH_TaskCenterTopView *)headerView{
    if (!_headerView) {
        _headerView = [[HNSH_TaskCenterTopView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth-24, JXTableHeaderViewHeight)];
        WeakSelf(weakSelf);
        _headerView.showAdVideoBlock = ^{
            [weakSelf gotoAdVideo];
        };
    }
    return _headerView;
}

- (JXCategoryTitleView *)categoryView{
    if (!_categoryView) {
        _categoryView = [[JXCategoryTitleView alloc] initWithFrame:CGRectMake(12, 0, kScreenWidth-24, JXheightForHeaderInSection)];
        _categoryView.backgroundColor = [UIColor whiteColor];
        _categoryView.delegate = self;
        _categoryView.titleSelectedColor = [UIColor colorWithHexString:@"#89030A"];
        _categoryView.titleColor = [UIColor colorWithHexString:@"#89030A"];
        _categoryView.titleFont = [UIFont systemFontOfSize:14];
        _categoryView.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
        _categoryView.titleColorGradientEnabled = YES;
        _categoryView.titleLabelZoomEnabled = YES;
        _categoryView.contentScrollViewClickTransitionAnimationEnabled = NO;
        _categoryView.listContainer = (id<JXCategoryViewListContainer>)self.pagerView.listContainerView;

        JXCategoryIndicatorLineView * lineView = [JXCategoryIndicatorLineView new];
        lineView.indicatorColor = [UIColor appGreenColor];
        lineView.indicatorWidth = 20;
        _categoryView.indicators = @[lineView];
    }
    return _categoryView;
}

//MARK: JXPagerViewDelegate Methods
- (UIView *)tableHeaderViewInPagerView:(JXPagerView *)pagerView{
    return self.headerView;
}

- (NSUInteger)tableHeaderViewHeightInPagerView:(JXPagerView *)pagerView{
    return JXTableHeaderViewHeight;
}

- (NSUInteger)heightForPinSectionHeaderInPagerView:(JXPagerView *)pagerView{
    return JXheightForHeaderInSection;
}

- (UIView *)viewForPinSectionHeaderInPagerView:(JXPagerView *)pagerView{
    return self.categoryView;
}

- (NSInteger)numberOfListsInPagerView:(JXPagerView *)pagerView{
    //和categoryView的item数量一致
    return self.categoryView.titles.count;
}

- (id<JXPagerViewListViewDelegate>)pagerView:(JXPagerView *)pagerView initListAtIndex:(NSInteger)index{
    HNSH_TaskCenterViewController * list = [HNSH_TaskCenterViewController new];
    list.type = index;
    return list;
    
}

//MARK: JXCategoryViewDelegate Methods
- (void)categoryView:(JXCategoryBaseView *)categoryView didSelectedItemAtIndex:(NSInteger)index{
    
}

//MARK: JXPagerMainTableViewGestureDelegate Methods
- (BOOL)mainTableViewGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    //禁止categoryView左右滑动的时候，上下和左右都可以滚动
    if (otherGestureRecognizer == self.categoryView.collectionView.panGestureRecognizer) {
        return NO;
    }
    return [gestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]] && [otherGestureRecognizer isKindOfClass:[UIPanGestureRecognizer class]];
}



@end
